﻿#include "MaterialDialogFluid.h"
#include "ui_MaterialDialogFluid.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsMaterialsFluid.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"
#include <QMessageBox>


namespace GUI
{
  

    MaterialFluidDialog::MaterialFluidDialog(PhysicsDlgExecType type, int matid /*= -1*/, QWidget* parent /*= nullptr*/)
        :AbstractMaterialDialog(type,parent)
    {
        _ui = new Ui::MaterialDialogFluid;
        _ui->setupUi(this);
        //获取材料对象
        if (_matMgr && matid > 0)
            _mat = dynamic_cast<Acoustics::FITKAcousticsMaterialsFluid*>(_matMgr->getDataByID(matid));
        
        this->init();

    }

    MaterialFluidDialog::~MaterialFluidDialog()
    {
        if (_ui) delete _ui;
    }

    void MaterialFluidDialog::on_pushButtonOK_clicked()
    {
        if(!this->checkInput()) return;
        //要处理的对象
        Acoustics::FITKAcousticsMaterialsFluid* fluid = dynamic_cast<Acoustics::FITKAcousticsMaterialsFluid*>(_mat);
        //保存执行的脚本
        QStringList str;
        QString name = _ui->lineEdit_Name->text();
        //创建与复制,创建新对象
        if (_execType == GUI::PhysicsDlgExecType::Create || _execType == GUI::PhysicsDlgExecType::Duplicate)
        {
            str.append("m=Material.Fluids()");
            str.append(QString("m.createMaterial('%1')").arg(name));

            fluid = new Acoustics::FITKAcousticsMaterialsFluid;
            _matMgr->appendDataObj(fluid);
        }
        if (!fluid) return;

        //保存数据
        double speed= _ui->lineEdit_SpeedOfSound->text().toDouble();
        double density= _ui->lineEdit_Density->text().toDouble();
        double kv= _ui->lineEdit_KinematicViscosity->text().toDouble();
        double rsh= _ui->lineEdit_RatioOfSpecificHeats->text().toDouble();
        double prandtl= _ui->lineEdit_PrandtlNumber->text().toDouble();
        double molecular= _ui->lineEdit_MolecularMass->text().toDouble();
        int type = 0;
        if (_ui->radioButton_Liquid->isChecked()) type = 1;

        if (_execType == GUI::PhysicsDlgExecType::Edit)
        {
            str.append(QString("m=Material.Fluids.GetFluids('%1')").arg(fluid->getDataObjectName()));
            str.append(QString("m.setName('%1')").arg(name));
        }
        str.append(QString("m.setFluidProp(%1,%2,%3,%4,%5,%6,%7)").arg(speed).arg(density).arg(kv).arg(rsh).arg(prandtl).arg(molecular).arg(type));

        fluid->setDataObjectName(name);
        fluid->setSpeedOfSound(speed);
        fluid->setDensity(density);
        fluid->setKinematicViscosity(kv);
        fluid->setSpecificHeatsRatio(rsh);
        fluid->setPrandtlNumber(prandtl);
        fluid->setMolecularMass(molecular);
        if (_ui->radioButton_Gas->isChecked())
            fluid->setType(Acoustics::FITKAcousticsMaterialsFluid::Gas);
        else if (_ui->radioButton_Liquid->isChecked())
            fluid->setType(Acoustics::FITKAcousticsMaterialsFluid::Liquid);

        this->saveScript(str);
        this->accept();
    }

    void MaterialFluidDialog::on_pushButtonCancel_clicked()
    {
        this->reject();
    }

    void MaterialFluidDialog::init()
    {
        if (_matMgr == nullptr) return;

        if (_execType == GUI::PhysicsDlgExecType::Create)
        {
            QString name = _matMgr->checkName("New Fluid Material");
            _ui->lineEdit_Name->setText(name);
        }

        if (_mat == nullptr ) return;
        Acoustics::FITKAcousticsMaterialsFluid* fluid = dynamic_cast<Acoustics::FITKAcousticsMaterialsFluid*>(_mat);
        if (!fluid) return;
        // 设置各个属性的值
        _ui->lineEdit_Name->setText(fluid->getDataObjectName());
        _ui->lineEdit_SpeedOfSound->setText(QString::number(fluid->getSpeedOfSound()));
        _ui->lineEdit_Density->setText(QString::number(fluid->getDensity()));
        _ui->lineEdit_KinematicViscosity->setText(QString::number(fluid->getKinematicViscosity()));
        _ui->lineEdit_RatioOfSpecificHeats->setText(QString::number(fluid->getSpecificHeatsRatio()));
        _ui->lineEdit_PrandtlNumber->setText(QString::number(fluid->getPrandtlNumber()));
        _ui->lineEdit_MolecularMass->setText(QString::number(fluid->getMolecularMass()));
        bool isGas = (fluid->getType() == Acoustics::FITKAcousticsMaterialsFluid::Gas) ? true : false;
        _ui->radioButton_Gas->setChecked(isGas);
        _ui->radioButton_Liquid->setChecked(!isGas);

        //复制,更新名称
        if (_execType == GUI::PhysicsDlgExecType::Duplicate)
        {
            QString text = QString("Copy of %1").arg(fluid->getDataObjectName());
            _ui->lineEdit_Name->setText(text);
        }
    }

    bool MaterialFluidDialog::checkInput()
    {
        //检查数据合法性
        QList<QLineEdit*> checkUi;
        checkUi << _ui->lineEdit_SpeedOfSound << _ui->lineEdit_Density << _ui->lineEdit_KinematicViscosity
            << _ui->lineEdit_RatioOfSpecificHeats << _ui->lineEdit_PrandtlNumber << _ui->lineEdit_MolecularMass;
        for (QLineEdit* e : checkUi)
        {
            if(e== nullptr) continue;
            QString text = e->text().trimmed();
            bool ok = false;
            text.toDouble(&ok);
            if (!ok)
            {
                QMessageBox::critical(nullptr, tr("Critical"), tr("Please enter a number."));
                return false;
            }
        }
        if (_matMgr == nullptr) return false;
        //检查名称
         QString text =_ui->lineEdit_Name->text().trimmed();
         if (text != "") {
             QString newText = _matMgr->checkName(text);
             if (text == newText) return true;
             //与自己重名
             if (_matMgr->getDataByName(text) == _mat) return true;
         }

         QMessageBox::critical(nullptr, tr("Critical"), tr("Please enter a new Name."));
         return false;
    }

}
